from fastapiApp.models.orders import Order_Mo
from fastapi.exceptions import HTTPException
from fastapiApp.schemas.order_scheams import OrderCreate

class OrderController:
    def __init__(self):
        self.model = Order_Mo

    async def is_exist(self, obj_in: OrderCreate) -> int:
        """检查订单是否存在"""
        if await self.model.filter(order_name = obj_in.order_name).exists():
            raise HTTPException(status_code=401, detail="订单名称已存在")
        return 0

    async def list(self, page: int = 1, page_size: int = 10):
        """获取订单分页列表"""
        query = await self.model.filter().offset((page - 1) * page_size).limit(page_size)
        count = await self.model.filter().count()

        data_list = [
            await data.to_dict() for data in query
        ]

        return count, data_list

# 创建控制器实例
order_controller = OrderController()
